import diff from "./diff"
import mountElement from "./mountElement"
export default function diffComponent(virtualDOM, oldComponent, oldDOM, container) {
    if (isSameComponent(virtualDOM, oldComponent)) {
        updateComponent(virtualDOM, oldComponent, oldDOM, container)
    } else {
        mountElement(virtualDOM, container, oldDOM)
    }
}


function isSameComponent(virtualDOM, oldComponent) {
    return oldComponent && virtualDOM.type === oldComponent.constructor
}

function updateComponent(virtualDOM, oldComponent, oldDOM, container) {
    oldComponent.componentWillReceiveProps(virtualDOM.props)
    let preProps = oldComponent.props
    if (oldComponent.shouldComponentUpdate(virtualDOM.props)) {
        oldComponent.componentWillUpdate(virtualDOM.props)
        oldComponent.updateProps(virtualDOM.props)
        let nextVDOM = oldComponent.render()
        nextVDOM.component = oldComponent
        diff(nextVDOM, container, oldDOM)
        oldComponent.componentDidUpdate(preProps)
    }
} 